-- DROP FUNCTION image_type_audit();

CREATE OR REPLACE FUNCTION image_type_audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO image_type_audit SELECT nextval('image_type_audit_seq'),'D', now(), OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO image_type_audit SELECT nextval('image_type_audit_seq'),'U', now(), NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO image_type_audit SELECT nextval('image_type_audit_seq'),'I', now(), NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION image_type_audit() OWNER TO postgres;

CREATE TRIGGER image_type_audit
    AFTER INSERT OR DELETE OR UPDATE ON image_type
    FOR EACH ROW
    EXECUTE PROCEDURE image_type_audit();